//
//  MineViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/17.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#define VIEW_HEIGHT    60

#define SM_BUTTON_HEIGHT 50

#import "MineViewController.h"
#import "SetViewController.h"
#import "LevelViewController.h"
#import "IntegralViewController.h"

#import "LLtalkVc.h" //客服

#import "MineViewModel.h"
#import "UserDetailViewController.h"
#import "MineCenterFooterView.h"


#import "AuthenticationViewController.h"
#import "UnbundlingViewController.h"
#import "UpdatePwdViewController.h"
#import "LotteryCollectViewController.h"
#import "PressCollectViewController.h"
#import "MyActivityViewController.h"
#import "AboutUsViewController.h"
#import "ContactViewController.h"
#import "LLexpertVC.h"


@interface MineViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    UIScrollViewDelegate,
    MineCenterFooterViewDelegate
>
{
    UIImageView *_headerImg;
    UILabel *_nameLabel;
    UIImageView * _levImg;
    UILabel * _signature;
    UIButton * buttonQiandao;
    UILabel * shenflabel;
}

@property (nonatomic,strong) UITableView * tableView;

@property(nonatomic,strong)UIImageView *backgroundImgV;
@property(nonatomic,assign)float backImgHeight;
@property(nonatomic,assign)float backImgWidth;
@property(nonatomic,strong)UIImageView *headImageView;

@property (nonatomic,strong) MineViewModel * mineViewModel;
@property (nonatomic,strong) MineCenterFooterView  * myCenterFooter;
@end

@implementation MineViewController

-(MineViewModel *) mineViewModel{
    if (!_mineViewModel) {
        _mineViewModel = [[MineViewModel alloc] init];
    }
    return _mineViewModel;
}


/**
 *  创建table底部
 */
-(MineCenterFooterView *) myCenterFooter{
    if (!_myCenterFooter) {
        
        _myCenterFooter = [[MineCenterFooterView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_RESIZE_UI(349))]; //打印bgview的来
        [_myCenterFooter setButtonDelegate:self];
    }
    return _myCenterFooter;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    
    [self usercenterInfoMessage];
    
    
    /**
     签到状态显示
     
     @param UserInfoShare.userCenterModel <#UserInfoShare.userCenterModel description#>
     @param userIsSign <#userIsSign description#>
     @return <#return value description#>
     */
    [RACObserve(UserInfoShare.userCenterModel, userIsSign) subscribeNext:^(id x) {
        
        NSLog(@" issign %ld",(long)[x integerValue]);
        
        if ([x integerValue] == 0) {
            [buttonQiandao setBackgroundImage:[UIImage imageNamed:@"qiandao"] forState:UIControlStateNormal];
            [buttonQiandao setEnabled:YES];
        }else{
            [buttonQiandao setBackgroundImage:[UIImage imageNamed:@"yiqiandao"] forState:UIControlStateNormal];
            [buttonQiandao setEnabled:NO];
        }
        
    }];
    
    
    
    /**
     是否实名认证
     
     @param UserInfoShare.userCenterModel <#UserInfoShare.userCenterModel description#>
     @param userIsBinding <#userIsBinding description#>
     @return <#return value description#>
     */
    [RACObserve(UserInfoShare.userCenterModel, userIsBinding) subscribeNext:^(id x) {
        
        if ([x integerValue] == 0) {
            [shenflabel setText:@"未绑定"];
        }
        else{
            [shenflabel setText:@""];
        }
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self backImageView];
    [self layoutTableView];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barBtnItemWithTitle:@"设置" target:self action:@selector(setClick)];
    
    /**
     签到
     */
    [self netWork];
    
}

-(void) netWork{
    
    /**
     签到请求
     
     @param x <#x description#>
     @return <#return value description#>
     */
    [[buttonQiandao rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        [MXUtility showMBProgress:self.view message:@"正在签到，请稍后～"];
        [[self.mineViewModel userSignRequestParameter] subscribeNext:^(id x) {
            
            
            if ([x[@"errorcode"] integerValue] == 0) {
                
                [MXUtility showTips:@"签到成功"];
            }
            
            [MXUtility hideMBProgress:self.view];
        }error:^(NSError *error) {
            
            [MXUtility hideMBProgress:self.view];
        }];
        
    }];
}

-(void) usercenterInfoMessage{
    [_headerImg sd_setImageWithURL:[NSURL URLWithString:UserInfoShare.userCenterModel.userImgPath] placeholderImage:DEF_NORMAL_DEFAULT_IMAGE];
    _nameLabel.text = UserInfoShare.userCenterModel.userName;
    [_signature setText:UserInfoShare.userCenterModel.userSign];
    [_levImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"dengji_vip%ld",(long)[UserInfoShare.userCenterModel.userLevelFlg integerValue]]]];
}


-(void) setClick{
    
    SetViewController * setView = [[SetViewController alloc] init];
    [self.navigationController pushViewController:setView animated:YES];
}

-(void) header_tap_Click:(UITapGestureRecognizer *) gesture{
    
    UserDetailViewController * detail = [[UserDetailViewController alloc] init];
    [detail setUserID:UserInfoShare.userCenterModel.userID];
    [self.navigationController pushViewController:detail animated:YES];
    
}

-(void)backImageView{
    UIImage *image=[UIImage imageNamed:@"ditu"];
    
    _backgroundImgV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, 130)];
    _backgroundImgV.image=image;
    _backgroundImgV.userInteractionEnabled=YES;
    [self.view addSubview:_backgroundImgV];
    _backImgHeight=_backgroundImgV.frame.size.height;
    _backImgWidth=_backgroundImgV.frame.size.width;
}

-(void)layoutTableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT - 49) style:UITableViewStylePlain];
        _tableView.backgroundColor=[UIColor clearColor];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.dataSource=self;
        _tableView.delegate=self;
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [self.view addSubview:_tableView];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:YES tabFooter:YES];
    }
    [_tableView setTableHeaderView:[self headImageView]];
    [_tableView setTableFooterView:[self myCenterFooter]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 12;
    }
    return 0;
}
#pragma mark -- 改变header头部的颜色
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = DEF_COLOR_F0F0F0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}



-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView=[[UIImageView alloc]init];
        _headImageView.frame=CGRectMake(0, 64, DEF_DEVICE_WIDTH, _backImgHeight + VIEW_HEIGHT);
        _headImageView.backgroundColor=[UIColor clearColor];
        _headImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *header = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(header_tap_Click:)];
        [_headImageView addGestureRecognizer:header];

        _headerImg=[[UIImageView alloc]initWithFrame:CGRectMake(35, 28, 50, 50)];
        _headerImg.backgroundColor=[UIColor clearColor];
        _headerImg.userInteractionEnabled=YES;
        UITapGestureRecognizer *header_tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(header_tap_Click:)];
        [_headerImg addGestureRecognizer:header_tap];
        [_headImageView addSubview:_headerImg];
        _headerImg.layer.cornerRadius = _headerImg.width / 2;
        _headerImg.layer.masksToBounds = YES;
        
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(_headerImg.x + _headerImg.width + 15, _headerImg.y + 5, 150, 20)];
        _nameLabel.userInteractionEnabled = YES;
        _nameLabel.textColor=[UIColor whiteColor];
        [_nameLabel setFont:[UIFont systemFontOfSize:15.f]];
        _nameLabel.textAlignment=NSTextAlignmentLeft;
        [_headImageView addSubview:_nameLabel];
        
        
        _levImg = [[UIImageView alloc] initWithFrame:CGRectMake(_headerImg.x + _headerImg.width + 15, _headerImg.y + _headerImg.height - 22, 50, 18)];
        [_headImageView addSubview:_levImg];
        
        
        _signature = [[UILabel alloc] initWithFrame:CGRectMake(_headerImg.x , _headerImg.y + _headerImg.height + 3, DEF_DEVICE_WIDTH - 70, 30)];
        [_signature setFont:[UIFont systemFontOfSize:13.f]];
        [_signature setTextAlignment:NSTextAlignmentLeft];
        [_signature setBackgroundColor:[UIColor clearColor]];
        [_signature setTextColor:[UIColor whiteColor]];
        [_headImageView addSubview:_signature];
        
        
        /*
         button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
         button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
         button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);*/
        buttonQiandao = [UIButton buttonWithType:UIButtonTypeSystem];
        [buttonQiandao setFrame:CGRectMake(DEF_DEVICE_WIDTH - 90, _backgroundImgV.height / 2 - 20, 90, 40)];
        [buttonQiandao.titleLabel setFont:[UIFont systemFontOfSize:12.f]];
        [_headImageView addSubview:buttonQiandao];
        
        
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, _headImageView.height - VIEW_HEIGHT, DEF_DEVICE_WIDTH, VIEW_HEIGHT)];
        [view setBackgroundColor:[UIColor clearColor]];
        
        
        //等级
        UIView * dengjiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH / 2, view.height)];
        [view addSubview:dengjiView];
        [dengjiView setBackgroundColor:[UIColor whiteColor]];
        
        
        UIImageView * djImg = [[UIImageView alloc] initWithFrame:CGRectMake(LTWPercentage(28), 12, LTWPercentage(35), LTWPercentage(35))];
        [djImg setImage:[UIImage imageNamed:@"icon_dengji"]];
        [dengjiView addSubview:djImg];
        
        UILabel * djLabel = [[UILabel alloc] initWithFrame:CGRectMake(djImg.x + djImg.width + 5, 0, LTWPercentage(dengjiView.width - djImg.width - 33), dengjiView.height)];
        [djLabel setText:@"等级中心"];
        [djLabel setFont:[UIFont systemFontOfSize:15.f]];
        [dengjiView addSubview:djLabel];
        
        //分割线
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(dengjiView.x + dengjiView.width, 5, 1, view.height - 10)];
        [lineView setBackgroundColor:DEF_COLOR_F0F0F0];
        [view addSubview:lineView];
        
        //积分
        UIView * jifenView = [[UIView alloc] initWithFrame:CGRectMake(DEF_DEVICE_WIDTH / 2, 0, DEF_DEVICE_WIDTH / 2, view.height)];
        [view addSubview:jifenView];
        [jifenView setBackgroundColor:[UIColor clearColor]];
        
        
        UIImageView * secondImg = [[UIImageView alloc]initWithFrame:CGRectMake(LTWPercentage(28), 12, LTWPercentage(35), LTWPercentage(35))];
        secondImg.image = [UIImage imageNamed:@"icon_jifen"];
        [jifenView addSubview:secondImg];
        
        
        UILabel * jfLabel = [[UILabel alloc] initWithFrame:CGRectMake(secondImg.x + secondImg.width + 5, 0, LTWPercentage(jifenView.width - secondImg.width - 33), jifenView.height)];
        [jfLabel setText:@"积分中心"];
        [jfLabel setFont:[UIFont systemFontOfSize:15.f]];
        [jifenView addSubview:jfLabel];
        
        
        
        [MXUtility bottomLayerborder:view borderColor:DEF_COLOR_F0F0F0];
        [_headImageView addSubview:view];
        
        
        /**
         等级跳转
         */
        UITapGestureRecognizer *djtap = [[UITapGestureRecognizer alloc] init];
        [djtap.rac_gestureSignal subscribeNext:^(id x) {
            
            NSLog(@"等级");
            LevelViewController * lev= [[LevelViewController alloc] init];
            [self.navigationController pushViewController:lev animated:YES];
            
        }];
        [dengjiView addGestureRecognizer:djtap];
        
        /**
         积分跳转
         */
        UITapGestureRecognizer * jftap = [[UITapGestureRecognizer alloc] init];
        [jftap.rac_gestureSignal subscribeNext:^(id x) {
            
            NSLog(@"积分");
            IntegralViewController * inte = [[IntegralViewController alloc] init];
            [self.navigationController pushViewController:inte animated:YES];
            
            
        }];
        [jifenView addGestureRecognizer:jftap];
        
    }
    return _headImageView;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat contentOffsety = scrollView.contentOffset.y;
    //实际上只是把图片的高度放大了
    
    if (contentOffsety<0) {
        CGRect rect = _backgroundImgV.frame;
        rect.size.height = _backImgHeight-contentOffsety;
        rect.size.width = _backImgWidth* (_backImgHeight-contentOffsety)/_backImgHeight;
        rect.origin.x =  -(rect.size.width-_backImgWidth)/2;
        rect.origin.y = 0;
        _backgroundImgV.frame = rect;
    }else{
        CGRect rect = _backgroundImgV.frame;
        rect.size.height = _backImgHeight;
        rect.size.width = _backImgWidth;
        rect.origin.x = 0;
        rect.origin.y = -contentOffsety;
        _backgroundImgV.frame = rect;
        
    }
}


/**
 九宫格下标

 @param tag <#tag description#>
 */
-(void)touchCenterButtonAction:(NSInteger)tag{
    
    if (tag == 1) {
        LotteryCollectViewController * lottery = [[LotteryCollectViewController alloc] init];
        [self.navigationController pushViewController:lottery animated:YES];
    }
    if (tag == 2) {
        PressCollectViewController * press = [[PressCollectViewController alloc] init];
        [self.navigationController pushViewController:press animated:YES];
    }

    if (tag == 3) {
        
        if ([UserInfoShare.userCenterModel.userIsBinding integerValue] == 0) {
            
            AuthenticationViewController * auth = [[AuthenticationViewController alloc] init];
            [self.navigationController pushViewController:auth animated:YES];
            
        }
        else{
            
            UnbundlingViewController * unbund = [[UnbundlingViewController alloc] init];
            [self.navigationController pushViewController:unbund animated:YES];
            
        }
    }
    if (tag == 4) {
        UpdatePwdViewController * updatePwd = [[UpdatePwdViewController alloc] init];
        [self.navigationController pushViewController:updatePwd animated:YES];

    }
    if (tag == 5) {
        MyActivityViewController * myactivity = [[MyActivityViewController alloc] init];
        [self.navigationController pushViewController:myactivity animated:YES];
    }
    
    if (tag == 6) {
        
        LLtalkVc *vc = [[LLtalkVc alloc]init];
        
        
        [[RCIMClient sharedRCIMClient]deleteMessages:ConversationType_CUSTOMERSERVICE targetId:@"KEFU149688800754484" success:^{
            
        } error:nil];
        
        vc.conversationType = ConversationType_CUSTOMERSERVICE;
        vc.targetId = @"KEFU149688800754484";
        
        vc.title = @"客服";
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (tag == 7) {
        
        LLexpertVC * ll = [[LLexpertVC alloc] init];
        [self.navigationController pushViewController:ll animated:YES];
    
    }
    if (tag == 8) {
        AboutUsViewController * about = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
    }
    if (tag == 9) {
        ContactViewController * cont = [[ContactViewController alloc] init];
        [self.navigationController pushViewController:cont animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
